<template>
  <div class="login-container">
    <div class="box1">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <div>
          <img src="../../assets/404_images/logotouxiang.png" alt="" class="login-logo">
        </div>
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.loginName"
            placeholder="Username"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="Password"
            name="password"
            tabindex="2"
            auto-complete="on"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-form-item class="auth-code" prop="code">
          <el-row :gutter="20">
            <el-col :span="16"><div class="grid-content bg-purple" />
              <el-input
                ref="code"
                v-model="loginForm.code"
                placeholder="请输入验证码"
                name="code"
                type="text"
                tabindex="3"
                auto-complete="new-code"
              />
            </el-col>
            <el-col :span="8"><div class="grid-content bg-purple" />
              <img id="canvas_img" src="" alt="code" class="code-img" @click="getCodeImg">
            </el-col>
          </el-row>
        </el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%; margin-top:30px;" @click="loginDeng">登录</el-button>

        <!-- <div class="tips">
          <span style="margin-right:20px;">username: admin</span>
          <span> password: any</span>
        </div> -->

      </el-form>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { codeImgAPI } from '@/api/login.js'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('账号出错'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('密码出错'))
      } else {
        callback()
      }
    }
    // const validatecode = (rule, value, callback) => {
    //   if (!validatecode(value)) {
    //     callback(new Error('验证码出错'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        code: '',
        loginType: 0,
        clientToken: 'Izc3vqqFtNl0A2KltIo4d63V3ePnT1GF'
      },
      loginRules: {
        loginName: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword },
          { min: 5, max: 16, required: true, message: '密码格式错误', trigger: 'blur' }],
        code: [{ required: true, trigger: 'blur', message: '请输入验证码' }]
      },
      loading: false,
      passwordType: 'password'
    }
  },
  created() {
    this.getCodeImg()
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    async getCodeImg() {
      const res = await codeImgAPI(this.loginForm.clientToken)
      console.log(res)
      const canvas_img = document.querySelector('#canvas_img')
      const imageType = res.headers['content-type'] // 获取图片类型
      const blob = new Blob([res.data], { type: imageType })
      const imageUrl = (window.URL || window.webkitURL).createObjectURL(blob)
      canvas_img.src = imageUrl
      canvas_img.onload = function(e) {
        window.URL.revokeObjectURL(canvas_img.src) // 释放URL.createObjectURL()创建的对象
      }
    },
    // 登录事件
    loginDeng() {
      // await dengLuAPI(this.loginForm)
      this.$refs.loginForm.validate((val) => {
        if (val) {
          this.$store.dispatch('user/loginActions', this.loginForm)
        }
      })
    }
  }

}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
    height: 47px;
    background: transparent;
    border: 0;
    border-radius: 0;
    padding: 12px 5px 12px 15px;
    color: #999;
    caret-color: #999;
    background-color: #fff;
    -webkit-appearance: none;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $cursor inset !important;
        -webkit-text-fill-color: $bg !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    // background: rgba(0, 0, 0, 0.1);
    border: #e2e2e2 1px solid;
    border-radius: 5px;
    color: #454545;
  }
  .auth-code{
    height: 52px;
    width: 446px;
    margin-bottom: 20px;
    line-height: 52px;
    border: #e2e2e2 1px solid ;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-repeat: no-repeat;
   background-size: cover;
  background-image: url(../../assets/404_images/lekede.jpg);
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    // padding: 160px 35px 0;
    margin: 0 auto;
    // overflow: hidden;
    // background-color: #fff;
    height: 400px;
    position: absolute;
    width: 518px;
    // height: 388px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 76px 35px 0;
    background: #fff;
    -webkit-box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    border-radius: 10px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;

      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
.box1{
  margin-top: 200px;
}
.login-logo{
  position: absolute;
    width: 96px;
    height: 96px;
    top: -46px;
    left: 50%;
    margin-left: -48px;
}
.ziti{
  color: #99a6c4;
}

</style>
